package org.op.nav;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import org.op.FrameView;
import org.op.editor.doc.EditorLoadController;
import org.op.menu.OpFileChooser;
import org.op.service.WwDocumentInfo;
import org.op.service.WwInfoObject;
import org.op.service.config.ConfigKeys;
import org.op.service.config.ConfigurationService;
import org.op.service.fileio.FileRWService;
import org.op.service.fileio.FileRefactoringService;
import org.op.service.log.LoggerService;
import org.op.util.MessageController;

public class DocumentNodeControllerImpl implements DocumentNodeController {

	private MessageController messageController;
	private FileRefactoringService refactoringService;
	private FrameView frameView;
	private NavigationView navView;
	private NavigationController navController;
	private FileRWService fileRWService;
	private EditorLoadController editorLoadController;
	private ConfigurationService configService;
	private LoggerService logger;

	public void trashNote() {

		// TODO - this is duplicate code (to trashFolder) -- put this in a
		// service
		try {
			// send out a confirmation dialog
			String trashMessage = messageController
					.getMessage("trash.note.confirmation");
			int confirm = JOptionPane.showConfirmDialog(frameView.getFrame(),
					trashMessage);

			// move the folder to the trash
			if (JOptionPane.YES_OPTION == confirm) {
				WwInfoObject parentFolderInfo = navView.getSelectedNodeObject();
				String path = parentFolderInfo.getFilepath();
				refactoringService.trashFile(path);

				// refresh the nav tree
				navController.reloadTree();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void addNote() {

		try {
			// Open a dialog to request the note name (no extensions, or special
			// characters allowed)
			String title = messageController.getLabel("add.wikiword");
			String message = messageController
					.getMessage("add.note.to.node.message");
			String noteName = JOptionPane.showInputDialog(null, message, title, 1);

			if (noteName != null) {
				// get the path to the given node
				WwInfoObject parentFolderInfo = navView.getSelectedNodeObject();
				String path = parentFolderInfo.getFilepath();
				path = path + ConfigurationService.FILE_SEP + noteName + ".txt";

				List<String> content = new ArrayList<String>();

				// persist an empty file to this path
				fileRWService.writeFileText(path, content);

				// refresh the navigation view
				navController.reloadTree();

				// add the empy document to the editor
				WwDocumentInfo docInfo = new WwDocumentInfo(noteName, path);
				editorLoadController.loadFileIntoEditor(content, docInfo);
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void moveNote() {
		try {
			WwInfoObject nodeInfo = navView.getSelectedNodeObject();

			// get the current directory of the document (or workspace, if a new file)
			String currentPath = nodeInfo.getFilepath();
			String workspacePath = configService.getProperty(ConfigKeys.KEY_CURRENT_WORKSPACE);
			
			// open the filechooser in this directory
			OpFileChooser folderChooser = new OpFileChooser(frameView.getFrame(), workspacePath);
			folderChooser.setFoldersOnly(true);
			folderChooser.setApproveButtonText(messageController.getLabel("button.move"));
			folderChooser.setTitle(messageController.getLabel("move.node.title"));
			File folder = folderChooser.openAndGet();
			
			if (folder!=null) {
				refactoringService.moveFile(currentPath, folder);
				
				// refresh the navigation tree
				navController.reloadTree();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public MessageController getMessageController() {
		return messageController;
	}

	public void setMessageController(MessageController messageController) {
		this.messageController = messageController;
	}

	public FileRefactoringService getRefactoringService() {
		return refactoringService;
	}

	public void setRefactoringService(FileRefactoringService refactoringService) {
		this.refactoringService = refactoringService;
	}

	public FrameView getFrameView() {
		return frameView;
	}

	public void setFrameView(FrameView frameView) {
		this.frameView = frameView;
	}

	public NavigationView getNavView() {
		return navView;
	}

	public void setNavView(NavigationView navView) {
		this.navView = navView;
	}

	public NavigationController getNavController() {
		return navController;
	}

	public void setNavController(NavigationController navController) {
		this.navController = navController;
	}

	public FileRWService getFileRWService() {
		return fileRWService;
	}

	public void setFileRWService(FileRWService fileRWService) {
		this.fileRWService = fileRWService;
	}

	public EditorLoadController getEditorLoadController() {
		return editorLoadController;
	}

	public void setEditorLoadController(
			EditorLoadController editorLoadController) {
		this.editorLoadController = editorLoadController;
	}

	public ConfigurationService getConfigService() {
		return configService;
	}

	public void setConfigService(ConfigurationService configService) {
		this.configService = configService;
	}

	public LoggerService getLogger() {
		return logger;
	}

	public void setLogger(LoggerService logger) {
		this.logger = logger;
	}
	
	

}
